#include "CStatusEffect.h"
#include "CStatusEffectHandler.h"
#include "CBaseEntity.h"
#include "CChar.h"
#include "CConsole.h"

CStatusEffectHandler * CStatusEffectHandler::_instance = NULL;

CStatusEffectHandler::CStatusEffectHandler() {
}

CStatusEffectHandler::~CStatusEffectHandler() {
}

CStatusEffectHandler * CStatusEffectHandler::getInstance() {

	if(_instance == NULL) {
		_instance = new CStatusEffectHandler();
	}
	return _instance;
}

void CStatusEffectHandler::initialize() {
	CStatusEffectHandler * statusHandler = getInstance();
	CreateThread(0,0,StatusTimeServ,statusHandler,0,0);
}

void CStatusEffectHandler::addEntity(CBaseEntity * entity) {
	CStatusEffectHandler * statusHandler = getInstance();
	unsigned long size = (unsigned long)statusHandler->m_entityStatusEffectList.size();
	for(unsigned long count = 0; count < size; count++) {
		CBaseEntity * tmpEntity = statusHandler->m_entityStatusEffectList.at(count);
		if(tmpEntity == entity) {
			CConsole::outDebOnly("Entity already in status effect handler list.");
			return;
		}
	}
	statusHandler->m_entityStatusEffectList.push_back(entity);
}

void CStatusEffectHandler::removeEntity(CBaseEntity * entity) {
	CStatusEffectHandler * statusHandler = getInstance();
	unsigned long size = (unsigned long)statusHandler->m_entityStatusEffectList.size();
	for(unsigned long count = 0; count < size; count++) {
		CBaseEntity * tmpEntity = statusHandler->m_entityStatusEffectList.at(count);
		if(tmpEntity == entity) {
			statusHandler->m_entityStatusEffectList.erase(statusHandler->m_entityStatusEffectList.begin()+count);
			return;
		}
	}
	CConsole::outDebOnly("Entity not in status effect handler list.");
}


DWORD WINAPI CStatusEffectHandler::StatusTimeServ(LPVOID lpParam)
{
	CStatusEffectHandler * statusHandler = (CStatusEffectHandler*)lpParam;
	unsigned char timeCounter = 0;
	while(1) {
		unsigned long size = (unsigned long)statusHandler->m_entityStatusEffectList.size();
		for(unsigned long count = 0; count < size; count ++) {
			CBaseEntity * pEntity = statusHandler->m_entityStatusEffectList.at(count);
			if(pEntity->isPlayer()) {
				if( ((CChar*)pEntity)->isHealing() ) {
					unsigned int diffTime = (unsigned int)((clock() - ((CChar*)pEntity)->m_healingStartTime)/1000);
					//CConsole::outDebOnly("DiffTime: %i",diffTime);
					if( diffTime >= 20 ) {
						if(diffTime%10 == 0) {
							// HP recovery starts at 10 after 20 seconds resting + 1 for each 10 seconds after
							unsigned short hp = 8 + (unsigned short)(diffTime / 10);
							// HP recovery starts at 12 after 20 seconds resting + 1 for each 10 seconds after
							unsigned short mp = 10 + (unsigned short)(diffTime / 10);

							unsigned short sumHP = (unsigned short)pEntity->getHp() + hp;
							unsigned short sumMP = (unsigned short)pEntity->getMp() + mp;
							
							if((pEntity->getMaxHp()) > sumHP) {
								pEntity->h.hp = sumHP;
							} else {
								pEntity->h.hp = pEntity->getMaxHp();
							}

							if((pEntity->getMaxMp()) > sumMP) {
								pEntity->h.mp = sumMP;
							} else {
								pEntity->h.mp = pEntity->getMaxMp();
							}

							pEntity->h.tp -= 10;
							if(pEntity->h.tp < 0)
								pEntity->h.tp = 0;
							
							((CChar*)pEntity)->updateHealth();
						}
					}
				}
			}
		}
		Sleep(1000);
		timeCounter++;
	}
	return 0;
}